<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-10 23:59:20 +0200 (Thu, 10 Jul 2008) $
 * $Rev: 1285 $
 * $Author: gavinspearhead $
 * $Id: parsenzb.php 1285 2008-07-10 21:59:20Z gavinspearhead $
 */
if (!defined ('ORIGINAL_PAGE'))
	define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathpn = realpath(dirname(__FILE__));

require_once "$pathpn/../functions/html_includes.php";


verify_challenge($_POST['challenge']);
$title = $LN['nzb_title'];
$timestamp = get_request('timestamp', '');
$time_int = NULL;

if ($timestamp != '') {
    $timestamp = $_POST['timestamp'];
    $time_int = strtotime($timestamp);
    if ($time_int === FALSE) {
        $timestamp = NULL;
        near_fatal_error($LN['browse_invalid_timestamp'], 'timestamp', NULL);
    } else if ($time_int < time() && $time_int > 0) { // the time is before now, so probably means tomorrow 
        $time_int += 24 * 60 * 60; // next day
        $timestamp .= ' +1 day';
    }
} else 
    $timestamp = NULL;


if(isset($_REQUEST['upload'])) {
    
	$url = trim($_REQUEST['upload']);
	if ($url == '') 
		Redirect('transfers.php');
	if (!is_uploaded_file($url)) 
		throw new exception($LN['error_filenotexec'].'(1)');

	$dlpath = get_dlpath($db) . NZB_PATH;
	if (isset($_REQUEST['upload_orig_filename']))
		$file_name = $_REQUEST['upload_orig_filename'];
	else
		$file_name = $url;
	if (substr($file_name, -4) == '.nzb') 
		$file_name = trim(substr($file_name,0, -4));
	$nzb_file = find_unique_name($dlpath, $username . '/', $file_name, '.nzb', TRUE);
	move_uploaded_file($url, $nzb_file);
	$url = $nzb_file;
} else if( isset($_REQUEST['url'])) {
	$url = trim ($_REQUEST['url']);
	if ($url == '') 
		Redirect('transfers.php');
/* Since when don't we allow local files anymore? IMO allow all files to be read, just don't display the content if it's not a valid file... */

	$pattern = '/((https?:\/\/)|(ftps?:\/\/))/i';
	if (preg_match($pattern, $url) == 0) {
		throw new exception($LN['error_filenotexec'].'(2)');
	}

} else if (isset($_REQUEST['file'])) {
	$url = realpath($_REQUEST['file']);
	if ($url == '') 
		Redirect('transfers.php');
/* This doesn't seem to work right either... needs some testing: */
	$dlpath = get_config($db, 'dlpath');
	if (substr($url,0, strlen($dlpath)) != $dlpath)
		throw new exception($LN['error_filenotexec'].'(3)');
} else
	throw new exception($LN['error_readnzbfailed']);


$rprefs = load_config($db);
$uc = new urdd_client($rprefs['urdd_host'],$rprefs['urdd_port'], $userID);

if (!$uc->is_connected())
	throw new exception($LN['error_urddconnect']);

$uc->parse_nzb($url, NULL, $time_int);
Redirect('transfers.php');
// Display 
if (!is_array($__message) OR count($__message < 1))
	$__message[] = $LN['parsenzb_error']; 

$smarty->assign('menu',		generate_menu($LN));
$smarty->assign('title',	$title);
$smarty->assign('__message',	$__message);
$smarty->display('transfers.tpl');

?>
